package controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;

import model.ID3Header;
import model.MP3File;

/**
 * this class has NO USE!
 * 
 *
 */
public class MyParser {

	/**
	 * This method parses a mp3 into a MP3File
	 * @param pathToFile
	 * @return	MP3File with all information gathered from the id3 tag
	 */
	public static MP3File parseFile(Path pathToFile) throws InvalidID3TagException {
		
		System.out.println("start parsing");
		MP3File mp3 = new MP3File();
		
		/*
		 * create file from path
		 */
		File file = pathToFile.toFile();
		boolean isFile = file.isFile();
		if(!isFile){
			System.out.println("Something is wrong");
			return null;
		}
		System.out.println("File created");
		
		/*
		 * read file into a byte array
		 * save header and content array into mp3file
		 */
		try(FileInputStream inputStream = new FileInputStream(file)) {         
			
			byte fileContent[] = new byte[(int)file.length()];
			System.out.println("Read file");
			inputStream.read(fileContent);
			inputStream.close();
			byte header[] = new byte[10];
			byte content[] = new byte[fileContent.length-10];
			
			System.out.println("Copy Header+Content");
			System.arraycopy(fileContent, 0, header, 0, 10);	//copy header
			System.arraycopy(fileContent, 10, content, 0, fileContent.length-10);	//copy content
			
			
			mp3.setHeader(header);
			mp3.setId3header(new ID3Header(header));
			mp3.setContent(content);
			
		}
		catch(IOException e) {
			e.printStackTrace();
		}
		
		
		
		return mp3;
	}
}
